define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'work/warranty/index' + location.search,
                   /* add_url: 'work/warranty/add',
                    edit_url: 'work/warranty/edit',*/
                    del_url: 'work/warranty/del',
                    multi_url: 'work/warranty/multi',
                    import_url: 'work/warranty/import',
                    table: 'work_warranty',
                }
            });
            var table = $("#table");
            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#final_price_sum").text(data.final_price_sum);
                $("#profit_sum").text(data.profit_sum);
                $("#receivable_sum").text(data.receivable_sum);
                $(".nav-tabs .active a").attr("data-value");
                var check=$(".nav-tabs .active a").attr("data-value");
                if(check==3 || check==4 || check==5 || check==6){
                    $('label[for="warranty_time"]').parent().css('display','none'); // 隐藏描述查询栏
                }
                $('label[for="equipment_id"]').parent().css('display','none');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                search:false,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'workID', title: __('WorkID'), operate: 'LIKE'},
                        {field: 'hospital.name', title: __('Hospital.name'), operate: 'LIKE', formatter: Table.api.formatter.normal},
                        {field: 'equipment.name', title: __('Equipment.name'), operate: 'LIKE',formatter: Controller.api.formatter.equip},
                        {field: 'equipment.marking', title: __('Equipment.marking'), operate: 'LIKE'},
                        {field: 'equipment.serial', title: __('Equipment.serial'), operate: 'LIKE'},
                        {field: 'sale.nickname', title: __('Sale.nickname'), operate: 'LIKE'},
                        {field: 'engineer.nickname', title: __('Engineer.nickname'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6'),"3":__('Status 3'),"7":__('Status 7')}, formatter: Table.api.formatter.status},
                        {field: 'billing_data', title: __('Billing_data'), searchList: {"0":__('Billing_data 0'),"1":__('Billing_data 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'cost_money', title: __('Cost_money'), operate:false},
                        {field: 'profit', title: __('Profit'), operate: false},
                        {field: 'xshk', title: __('Xshk'), operate: false},
                        {field: 'receivable', title: __('Receivable'), operate: false},
                        {field: 'final_price', title: __('Final_price'), operate:false},
                        {field: 'collect_data', title: __('collect_data'), searchList: {"0":__('Collect_data 0'),"1":__('Collect_data 1')}, formatter: Table.api.formatter.normal},
                        {field: 'open_data', title: __('Open_data'), searchList: {"0":__('Open_data 0'),"1":__('Open_data 1')}, formatter:function (value, row, index) {
                                //Table.api.formatter.toggle;
                                if(row["status"]==2 || row["status"]==3)
                                {
                                    return '已关闭';
                                }else{
                                    var that = $.extend({}, this);
                                    return Table.api.formatter.toggle.call(that, value, row, index);
                                }
                            }
                        },
                        {field: 'warranty_time', title: __('Warranty_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        /*{field: 'equipment.enclosure', title: __('Equipment.enclosure'), operate: 'LIKE'},
                        {field: 'equipment.software_version', title: __('Equipment.software_version'), operate: 'LIKE'},*/
                        {field: 'contacts', title: __('Contacts'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        //{field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE',formatter: Table.api.formatter.normal},
                        //{field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'equipment_id', title: __('Equipment_id'), operate: '=', formatter: Table.api.formatter.normal},
                       /* {field: 'comm_data', title: __('Comm_data'), searchList: {"0":__('Comm_data 0'),"1":__('Comm_data 1')}, formatter: Table.api.formatter.normal},
*/
                        {field: 'buttons', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'xshk',
                                    title: __('回款记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '回款记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/xshk?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'cost',
                                    title: __('成本'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '成本',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='cost/cost?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'offer',
                                    title: __('报价'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '报价',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/offer?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'project',
                                    title: __('方案'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '方案',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/project?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'repair',
                                    title: __('维修'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '维修',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/repair?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'maintain',
                                    title: __('保养'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '保养',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/maintain?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'maintain',
                                    title: __('工作流'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '工作流',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='workflow?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'service',
                                    title: __('维修记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '维修记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/service?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'commission',
                                    title: __('提成记录'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    text: '提成记录',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='work/commission?warranty_id='+row.id;
                                        return url;
                                    },
                                },
                                {
                                    name: 'report',
                                    title: __('服务报告'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '服务报告',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='report/index?warranty_id='+row.id;
                                        return url;
                                    },
                                }
                            ],
                            formatter: Table.api.formatter.buttons, operate: false
                        },
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'add_image',
                                    title: __('报价'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('报价'),   icon: '',
                                    url: 'work/offer/add'
                                },{
                                    name: 'add_commin',
                                    title: __('提成'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('提成'),   icon: '',
                                    url: function (row) {
                                        var url='work/commission/add?work_warranty_id='+row["id"];
                                        return url;
                                    },
                                    visible: function (row) {
                                        // console.log(row)
                                        var check=$(".nav-tabs .active a").attr("data-value");
                                        console.log(check)
                                        if(check!=5 && check!=6 ){
                                            return false;
                                        }
                                        return true;

                                    },
                                },{
                                    name: 'loss',
                                    title:'亏损',
                                    text: '亏损',
                                    classname: 'btn btn-xs btn-primary btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定改为亏损？',
                                    url: 'work/warranty/loss',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    visible: function (row) {
                                         //console.log(row)
                                        var check=$(".nav-tabs .active a").attr("data-value");
                                        if(row.is_loss==1){
                                            return false;
                                        }
                                        /*if(row.is_loss==1){
                                            return false;
                                        }*/
                                        return true;

                                    },
                                },/*{
                                    name: 'comm',
                                    title:'改为已提成',
                                    text: '改为已提成',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: '',
                                    confirm: '确定改为已提成？',
                                    url: 'work/warranty/comm',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                        return false;
                                    },
                                },*/
                                {
                                    name: 'see',
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('详情'),   icon: '',
                                    url: 'work/warranty/see'
                                },
                                {
                                    name: 'appoint',
                                    title: __('指派'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('指派'),   icon: '',
                                    extend:'data-area=["1000px","800px"]',
                                    url: 'work/warranty/appoint',
                                },
                                {
                                    name: 'appoint2',
                                    title: __('改派'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    text: __('改派'),   icon: '',
                                    extend:'data-area=["1000px","800px"]',
                                    url: 'work/warranty/appoint?type=1',
                                    visible: function (row) {
                                        if(row.engineer_id==0 || row.sale_id==0){
                                            return false;
                                        }
                                        return true;

                                    },
                                },
                                {
                                    name: 'final2',
                                    extend:'data-area=["800px","400px"]',
                                    title: __('填写成交价'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('填写成交价'),   icon: '',
                                    url: 'work/warranty/final?type=1',
                                    visible: function (row) {
                                        //console.log(row)
                                        if(Number(row.final_price)>0){
                                            return false;
                                        }
                                        return true;

                                    },
                                },
                                {
                                    name: 'final',
                                    extend:'data-area=["800px","400px"]',
                                    title: __('修改成交价'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    text: __('修改成交价'),   icon: '',
                                    url: 'work/warranty/final',
                                    visible: function (row) {
                                        if(Number(row.final_price)<=0){
                                            return false;
                                        }
                                        return true;

                                    },
                                },
                                {
                                    name: 'billing',
                                    title: __('开票'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    text: __('开票'),   icon: '',
                                    url: 'work/warranty/billing'
                                }
                            ],
                        events: Table.api.events.operate,
                            formatter:function (value, row, index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                /*if(row["engineer_id"]!=0)
                                {
                                    $(table).data("operate-appoint", null);
                                    that.table = table;
                                }*/
                                if(row["status"]>=2 || row.engineer_id!=0 && row.sale_id!=0){
                                    $(table).data("operate-appoint", null);
                                    that.table = table;
                                }
                                if(row["status"]>=2)
                                {
                                    $(table).data("operate-appoint", null);
                                    $(table).data("operate-appoint2", null);
                                    that.table = table;
                                }
                                if(row["comm_data"]==1)
                                {
                                    $(table).data("operate-comm", null);
                                    that.table = table;
                                }
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },}

                    ]
                ]
            });
            table.bootstrapTable('hideColumn','equipment_id');
            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("work/warranty/export") + "' method='post' target='_blank'>" +
                    "<input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'>" +
                    "<input type='hidden' name='search'><input type='hidden' name='columns'>" +
                    "</form>", {
                    title: '导出数据',
                    btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    }
                    , yes: function (index, layero) {
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), layero);
                        return false;
                    }
                    ,
                    btn3: function (index, layero) {
                        submitForm("all", layero);
                        return false;
                    }
                })
            });
            var submitForm = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                console.log(search);
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
            };
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        appoint: function () {
             Controller.api.bindevent();
            // 初始化表格参数配置
            Table.api.init({
                search: false,
                showToggle: false,
                pagination: true,
                showColumns: false,
                showExport: false,
                extend: {
                    "index_url": "work/warranty/designate"+ location.search+"&warranty_id="+$("#warranty_id").val(),
                    "add_url": "",
                    "edit_url": "",
                    "del_url": "",
                    "multi_url": "",
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'engineer.nickname', title: __('工程师')},
                        {field: 'sale.nickname', title: __('销售')},
                        {field: 'createtime', title: __('时间')},
                    ]
                ],
                commonSearch: false
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on('click', "#btn_query", function () {
                //var id=$("#id_value").val();
                var opt = {
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                };
                table.bootstrapTable('refresh',opt);
                return false;
            });
             $(document).on('click', ".btn-refresh", function () {
                //var id=$("#id_value").val();
                var opt = {
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                };
                table.bootstrapTable('refresh',opt);
                return false;
            });
            
        },
        billing: function () {
            Controller.api.bindevent();
        },
        final: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                equip: function (value, row, index) {
                    //这里手动构造URL
                    url = "equipment?id=" +  row.equipment.id;
                    this.url = url;
                    return Table.api.formatter.addtabs.call(this, value, row, index);
                }
            }
            /*,
            formatter: {
                ip: function (value, row, index) {
                    //这里手动构造URL
                    url = "example/bootstraptable?" + this.field + "=" + value;

                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    //return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("Search %s", value) + '">' + __('Search %s', value) + '</a>';

                    //方式二,直接调用Table.api.formatter.addtabs
                    this.url = url;
                    return Table.api.formatter.addtabs.call(this, value, row, index);
                }
            }*/
        }
    };
    return Controller;
});
$(".nav-tabs li").click(function () {
    var val=$(this).children("a").attr("data-value");
    if(val==4 || val==''){
        $(".btn-export").removeClass("hide");
    }else{
        $(".btn-export").addClass("hide");
    }
})